import { cancelAdvanceApi } from "@/api/client/documentManagement/advanceManagement";
import { getWaybillByPreOderIdApi } from "@/api/client/documentManagement/waybillManagement";
import { message } from "ant-design-vue";
export default function OperationHook(state: Base.GeneraDataType) {
  const { tableData, loading, emit, selectRows, passData } = state;
  const init = (id: string) => {
    loading.value = true;
    return getWaybillByPreOderIdApi(id)
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          tableData.value = data || [];
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  const batchCancelHandle = () => {
    if (!selectRows.value) return message.error("请先勾选运单!");
    loading.value = true;
    return cancelAdvanceApi({
      preOrderId: passData.value.id,
      shipmentIdList: selectRows.value.map((t: any) => t.id)
    })
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          init(passData.value.id);
        }
      })
      .finally(() => (loading.value = false));
  };
  const singleCancelHandle = (row: Base.GeneraDataType) => {
    loading.value = true;
    return cancelAdvanceApi({
      preOrderId: passData.value.id,
      shipmentIdList: [row.id]
    })
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          init(passData.value.id);
        }
      })
      .finally(() => (loading.value = false));
  };
  const cancelHandle = () => {
    emit("update:visible", false);
  };
  return {
    init,
    batchCancelHandle,
    cancelHandle,
    singleCancelHandle
  };
}
